const {updateUserInfo,getUserInfoById} = require('../service/userInfo')
const {SuccessModel, ErrorModel} = require('../model/ResModel')
const {addRoleFailInfo} = require('../model/ErrorInfo')

const patchUserInfo = async (ctx, {firstName, lastName, email, description, gender, picture, city}) => {
  const {id} = ctx.session.user.user

  const result = await updateUserInfo({firstName, lastName, email, description, gender, picture, city, uid: id})
  if (result) {
    const newUserInfo = await getUserInfoById(id)
    ctx.session.user.userInfo = newUserInfo
    return new SuccessModel(newUserInfo)
  } else {
    return new ErrorModel(addRoleFailInfo)
  }
}

module.exports = {
  patchUserInfo
}
